﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace hoangnam12
{
    public partial class GioHang : System.Web.UI.Page
    {
        static DataTable tbGioHang = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["GioHang"] != null)
                {
                    tbGioHang = (DataTable)Session["GioHang"];
                    GridView1.DataSource = tbGioHang;
                    GridView1.DataBind();
                    total();
                }
                else
                {
                    lblthongbao.Text = "Bạn chưa chọn mua sản phẩm nào";
                }
            }
        }

        public void total()
        
        {
            DataTable dt = new DataTable();
            
            dt = (DataTable)Session["GioHang"];
            double tongGia = 0;
            foreach (DataRow dr in dt.Rows) //lay tat ca gia tri trong bang
            {
                tongGia += double.Parse(dr[4].ToString());
            }
            GridView1.FooterRow.Cells[3].Text = tongGia.ToString();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //string str = GridView1.DataKeys[e.RowIndex].Value.ToString();// lay id san pham        
            string str = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string str1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSoLuong")).Text;//lay gia tri trong control soluong
            DataTable dt = new DataTable();
            dt = (DataTable)Session["GioHang"];
            //string name = DB.LayTenSP(Convert.ToString(str));//lay ten san pham dua nao id san pham o tren
            if (dt.Rows.Count != 0)//kt bang
            {
                foreach (DataRow dr in dt.Rows)//lap tat ca? gia tri co trong row
                {
                    if (dr[0].ToString().Equals(str))//kiem tra xem no trung voi gia tri nao
                    {
                        dr[3] = double.Parse(str1);//update vao datatablse voi gia tri vua sua
                        dr[4] = Convert.ToInt32(dr[2]) * Convert.ToInt32(dr[3]);//update lai cot, thanh tien
                    }
                }
            }
            Session["GioHang"] = dt;//luu lai vao session
            GridView1.DataSource = dt;
            GridView1.DataBind();
            total();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            tbGioHang = (DataTable)Session["GioHang"];
            tbGioHang.Rows.RemoveAt(e.RowIndex);
            Session["GioHang"] = tbGioHang;
            GridView1.DataSource = tbGioHang;
            GridView1.DataBind();
            total();

            if (tbGioHang.Rows.Count == 0)
            {

                lblthongbao.Text = "Không còn mặt hàng nào trong giỏ";
                Session["GioHang"] = null;
            }
        }




        protected void Button3_Click(object sender, EventArgs e)
        {
            Session["GioHang"] = null;
            
            GridView1.DataSource = (DataTable)Session["GioHang"];
            GridView1.DataBind();
            lblthongbao.Text = "Hủy Thành Công!";
        }




        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }








        protected void Button1_Click1(object sender, EventArgs e)
        {
            if (Session["GioHang"] != null)
            {
                tbGioHang = (DataTable)Session["GioHang"];
                DateTime ngay = DateTime.Now.Date;
                dataprovider da = new dataprovider();
                string tenkh = Session["login"].ToString();
                string sql = "select MaKhach from [User] where Username = '" + tenkh + "'";
                SqlDataReader reader = da.ExecuteReader(sql);
                string makh = null;
                while (reader.Read())
                {
                    makh = String.Format("{0}", reader[0]);
                }
                //Console.Write(makh);

                foreach (DataRow dr in tbGioHang.Rows)
                {
                    string ma_sp = dr["idSP"].ToString();
                    string ten_sp = dr["TenSP"].ToString();
                    int don_gia = Convert.ToInt32(dr["Gia"].ToString());
                    int so_luong = Convert.ToInt32(dr["SoLuong"].ToString());
                    int thanhtien = don_gia * so_luong;
                    try
                    {
                        string sql1 = "insert into HoaDon values('','" + makh + "','" + ngay + "','')";

                        da.Executenonquery(sql1);
                    }
                    catch
                    {
                        lblthongbao.Text = "Đặt hàng Không thành công";
                    }

                    finally
                    {

                        string sql3 = "select SoHD from HoaDon where MaKhach ='" + makh + "'";
                        SqlDataReader reader1 = da.ExecuteReader(sql3);
                        string sohd = null;

                        while (reader1.Read())
                        {
                            sohd = String.Format("{0}", reader1[0]);
                        }
                        string sql2 = "insert into ChiTietHoaDon values ('" + sohd + "','" + ma_sp + "','" + so_luong + "','" + don_gia + "')";
                        da.Executenonquery(sql2);
                        lblthongbao.Text = "Đặt hàng thành công";
                        Session["GioHang"] = null;
                        GridView1.DataSource = (DataTable)Session["GioHang"];
                        GridView1.DataBind();
                    }
                }
            }

            else
            {
                lblthongbao.Text = "Trong Giỏ Hàng Chưa Có Sản Phẩm";
            }



        }
           

        

       


   
        
        
        
        
  




     
    }
}